#!/usr/bin/env python
# -*- coding:utf-8 -*-
import threading

import requests
import sys

reload(sys)
sys.setdefaultencoding('utf8')


def getbaidu(ip):
    head = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36'}
    url = 'https://www.baidu.com/'  # 你用于测试自己ip的网站
    try:
        print("start请求")
        request = requests.get(url, proxies={'http': 'http://'+ip, 'https': 'https://'+ip}, headers=head, timeout=10)  # 让问这个网页  随机生成一个ip
        request.encoding = request.apparent_encoding  # 设置编码 encoding 返回的是请求头编码  apparent_encoding 是从内容网页中分析出的响应内容编码方式
        print(request.text)  # 输出返回的内容
        filename = "baidu.html"
        with open(filename, 'wb') as f:
            f.write(request.text)
        print ("代理请求成功")
    except:
        print ("代理请求失败")
    print ("****************************************")

getbaidu("116.62.156.137:80")